/**
 * 
 */
package com.google.code.cisx.client.service;

import com.google.code.cisx.client.dto.CommonViewHelper;
import com.google.code.cisx.client.dto.ProjectViewHelper;
import com.google.code.cisx.client.dto.QueryViewHelper;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.ServiceDefTarget;

import java.util.List;

/**
 * RemoteService interface that the {@link RemoteService} implementations
 * dealing with Model entities are expected to implement.
 * <p>
 * TODO: Fix Javadocs for this RemoteService interface!!
 * 
 * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a>
 * @since 1.0
 * @version $Id: RemoteProjectService.java 94 2008-01-05 02:49:05Z
 *          rahul.thakur.xdev $
 */
public interface RemoteProjectService extends RemoteService {
  /**
   * Looks up the underlying store and returns a {@link T} instance that matches
   * the specified id.
   * 
   * @param klass {@link Class} for type entity to lookup and return an instance
   *          of.
   * @param id Entity Type {@link T}'s id to match.
   * 
   * @return matching entity type {@link T} instance.
   * @throws RemoteServiceException
   * 
   */
  public ProjectViewHelper lookup(Long id) throws RemoteServiceException;

  /**
   * Persists the passed in entity type {@link T} instance to the underlying
   * store.
   * <p>
   * If the entity instance already exists in the database it is updated, else a
   * new instance is created and an store-generated identifier assigned to it.
   * 
   * @param entity Type {@link T} instance to be created/saved.
   * @return updated entity type {@link T} instance.
   * @throws RemoteServiceException if there was an error saving the entity.
   */
  public ProjectViewHelper save(ProjectViewHelper entity) throws RemoteServiceException;

  /**
   * Removes the passed entity type {@link T} instance from the underlying
   * store.
   * 
   * @param entity Type {@link T} instance to remove.
   * @throws RemoteServiceException if there was an error removing the entity.
   */
  public void delete(ProjectViewHelper entity) throws RemoteServiceException;

  /**
   * Obtains a {@link List} of instances of entity type {@link T} which match
   * the criteria specified by the passed in query instance.
   * 
   * @param query instance that wraps up the criteria for querying matching
   *          instances in the system.
   * @param startIdx index for first result.
   * @param range number of results to fetch starting from first result.
   * @return {@link List} of instances of entity type {@link T} which match the
   *         specified query.
   * @throws RemoteServiceException
   */
  public List<? extends ProjectViewHelper> query(QueryViewHelper<ProjectViewHelper> query,
      int startIdx, int range) throws RemoteServiceException;

  public static class Util {

    private static RemoteProjectServiceAsync asyncInstance;

    public static RemoteProjectServiceAsync getAsyncInstance() {
      if (null == asyncInstance) {
        asyncInstance = GWT.create(RemoteProjectService.class);
        // TODO: Set up other properties for the Async service instance
        // before returning to browser client.

        ((ServiceDefTarget) asyncInstance).setServiceEntryPoint(GWT.getModuleBaseURL()
            + "cisx/RemoteProjectService");

      }
      return (RemoteProjectServiceAsync) asyncInstance;
    }
  }
}
